package com.zhang.findkeyword;

import com.zhang.findkeyword.utils.BashUtils;

import java.util.*;

/**
 * @author Alger
 * @date 2021/11/11  15:35
 */
public class FindKeyword {

    public static void main(String[] args) {
        HashMap<String, List<String>> ListHashMap = new HashMap<>();
        List<String> fileList = getFileName("cic");

        List<String> fileAndWordsList = getFileAndWords("cic");


        for (String filename : fileList) {
            List<String> wordlist = new ArrayList<>();
            ListIterator<String> fileAndWordsListIterator = fileAndWordsList.listIterator();
            while (fileAndWordsListIterator.hasNext()) {
                String fileAndWords = fileAndWordsListIterator.next();
                String words = null;
                String[] wordsSplit=null;
                if (fileAndWords.contains(filename)) {
                    words = fileAndWords.split(filename + ":")[1];
                    wordsSplit= words.split(",");
                    for (String word : wordsSplit) {
                        if (word.contains("cic")){
                            wordlist.add(word);
                        }
                    }
                }
                wordlist = new ArrayList<String>(new TreeSet<String>(wordlist));
            }
            ListHashMap.put(filename,wordlist);
        }

        System.out.println(ListHashMap);

    }

    public static List<String> getFileName(String words) {
        List<String> fileList = BashUtils.executeWithPipelineReListWithError("grep -R -l " + words + " /gEye/home/lib/find_keyword*");
        return fileList;
    }

    public static List<String> getFileAndWords(String words) {
        List<String> wordList = BashUtils.executeWithPipelineReListWithError("grep -R " + words + " /gEye/home/lib/find_keyword*");
        return wordList;
    }


}
